<template>
  <div class="art_container">
    <el-row :gutter="20">
      <el-col :span="18" :xs="24" :sm="17">
        <el-card>
          <div class="atr_banner">
            <div
              class="banner_container"
              :style="{
                backgroundImage: 'url(' + article.first_picture + ') ',
                backgroundRepeat: 'no-repeat',
                backgroundSize: '100% 100%',
              }"
            ></div>
            <div class="banner_container_title">
              <h1>{{ article.title }}</h1>
              <p>{{ article.create_time | dataFormat }}</p>
            </div>
          </div>
          <div class="blog_content">
            <div
              v-highlight
              :style="styles"
              class="article-content markdown-body"
              v-html="articleHtml"
              ref="article"
            ></div>

            <!-- <md-preview
            :content="article.content"
            :is-bg-show="true"
            class="article-content markdown-body"
          /> -->
          </div>
          <div class="essay_pre_next">
            <p v-if="article.lastArticle">
              上一篇:
              <router-link :to="{ path: `/article/${article.lastArticle.id}` }">
                <span>{{ article.lastArticle.title }}</span>
              </router-link>
            </p>

            <p v-if="article.nextArticle">
              下一篇:
              <router-link :to="{ path: `/article/${article.nextArticle.id}` }"
                ><span>{{ article.nextArticle.title }}</span>
              </router-link>
            </p>
          </div>
        </el-card>
        <el-card style="margin-top: 15px">
          <comment
            :commentList="commentList"
            :count="count"
            @reloadComment="getBlogComment"
          ></comment>
        </el-card>
      </el-col>
      <el-col :span="6" :xs="24" :sm="7">
        <div style="position: sticky; top: 60px">
          <el-card class="right-container">
            <div class="right-title">
              <i class="iconfont iconhanbao" style="font-size: 16.8px" />
              <span style="margin-left: 10px">目录</span>
            </div>
            <div id="toc" />
          </el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
// import MdPreview from "@/components/article/ArticleContent/index.vue";
import Comment from "@/components/common/comment.vue";
import { getBlogContent, getBlogComment } from "@/request/api";
import tocbot from "tocbot";
import marked from "marked";
import highlight from "highlight.js";
import "github-markdown-css/github-markdown.css";
import "highlight.js/styles/atom-one-dark.css";

marked.setOptions({
  renderer: new marked.Renderer(),
  gfm: true,
  breaks: true,
  headerPrefix: "header-nav",
  highlight(code) {
    return highlight.highlightAuto(code).value;
  },
});

export default {
  name: "",
  components: { Comment },
  props: [""],
  directives: {
    highlight(el) {
      const blocks = el.querySelectorAll("pre code");
      blocks.forEach((block) => {
        highlight.highlightBlock(block);
      });
    },
  },
  computed: {
    articleHtml() {
      const renderer = new marked.Renderer();
      let index = 0;
      renderer.heading = function (text, level) {
        return `<h${level} id="titleAnchor-${index++}">${text}</h${level}>`;
      };
      // this.content = this.article.content;
      return marked(this.article.content || "", { renderer });
    },
    styles() {
      return {
        padding:
          typeof this.padding === "number" ? `${this.padding}px` : this.padding,
      };
    },
  },
  data() {
    return {
      article: {},
      isBgShow: false,
      content: "",
      padding: "10px",
      commentList: [],
      count: 0,
    };
  },
  created() {
    this.getBlogInfomation();
    this.getBlogComment();
  },
  mounted() {},
  methods: {
    // 获取博客详情信息
    async getBlogInfomation() {
      const { data: res } = await getBlogContent({
        id: this.$route.params.articleId,
      });
      this.article = res.data;
      this.$nextTick(() => {
        tocbot.init({
          tocSelector: "#toc", //要把目录添加元素位置，支持选择器
          contentSelector: ".article-content", //获取html的元素
          headingSelector: "h1, h2, h3,h4", //要显示的id的目录
          hasInnerContainers: true,
          onClick: function (e) {
            e.preventDefault();
          },
        });
      });
    },
    async getBlogComment() {
      const { data: res } = await getBlogComment({
        id: this.$route.params.articleId,
      });
      if (res.status == 200) {
        this.commentList = res.data;
        this.count = this.commentList.length;
        // console.log(this.commentList);
      }
    },
  },
};
</script>
<style lang='less' scoped>
.art_container {
  max-width: 1200px;
  margin: 60px auto;
}
.banner_container {
  width: 100%;
  height: 100%;
  // background: url("@/assets/image/c.jpg") no-repeat;
  filter: blur(3px);
}
.atr_banner {
  height: 250px;
  position: relative;
}
.banner_container_title {
  //   display: inline-table;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 20px;
  width: 100%;
  vertical-align: middle;
  text-align: center;
  h1 {
    font-size: 28px;
    font-weight: normal;
    margin-bottom: 5px;
    color: #fff;
    text-shadow: 0 0 3px rgb(0 0 0 / 50%);
  }
  p {
    display: inline-block;
    color: #fff;
    font-size: 14px;
    padding: 2px 8px;
    border-radius: 5px;
    background-color: rgba(0, 0, 0, 0.2);
    margin-bottom: 10px;
  }
}
.article-content {
  word-break: break-word;
  font-size: 14px;
  line-height: 2;
}
.markdown-body p {
  text-align: justify;
}
.essay_pre_next {
  line-height: 34px;
  // margin-bottom: 20px;
  overflow: hidden;
  display: flex;
  justify-content: space-between;
  margin: 10px 10px 20px 10px;
}
.essay_pre_next p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.essay_pre_next a {
  margin-left: 10px;
  font-weight: bold;

  color: #001737;
}
.right-container {
  padding: 20px 24px;
  font-size: 14px;
}
.right-title {
  display: flex;
  align-items: center;
  line-height: 2;
  font-size: 16.8px;
  margin-bottom: 6px;
  font-weight: bold;
}

.right-title i {
  font-weight: bold;
}
/deep/ .toc-list-item {
  line-height: 2;
  list-style: none;
}
/deep/ .toc-link {
  display: block;
  border-left: 3px solid transparent;
  padding-left: 6px;
  color: #666261 !important;
  transition: all 0.2s ease-in-out;
}
/deep/ .is-active-link {
  border-left-color: #009d92;
  background: #00c4b6;
  color: #fff !important;
}
/deep/ .el-col-6 {
  height: 1600px;
}
.right-container /deep/ ol {
  padding-left: 0px !important;
}
.right-container /deep/.is-collapsible {
  padding-left: 8px !important;
}
</style>